﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CLEX.AdminUI.Service;
using CLEX.AdminUI.View;

namespace CLEX.AdminUI.ViewModel
{
    internal sealed class SharedDatabaseListVM : ViewModelBase
    {
        private readonly Facade facade;
        private readonly ObservableCollection<SharedDatabaseReadVM> sharedDatabases = new ObservableCollection<SharedDatabaseReadVM>();
        private SharedDatabaseReadVM selectedSharedDatabase;
        private RelayCommand newSharedDatabase;
        private RelayCommand editSharedDatabase;
        private RelayCommand deleteSharedDatabase;

        public IEnumerable<SharedDatabaseReadVM> SharedDatabases
        {
            get { return sharedDatabases; }
        }

        public SharedDatabaseReadVM SelectedSharedDatabase
        {
            get { return selectedSharedDatabase; }
            set
            {
                if (selectedSharedDatabase != value)
                {
                    selectedSharedDatabase = value;
                    FirePropertyChanged("SelectedSharedDatabase");
                }
            }
        }

        public ICommand NewSharedDatabase
        {
            get
            {
                if (newSharedDatabase == null)
                {
                    newSharedDatabase = new RelayCommand(DoNewSharedDatabase);
                }
                return newSharedDatabase;
            }
        }

        public ICommand EditSharedDatabase
        {
            get
            {
                if (editSharedDatabase == null)
                {
                    editSharedDatabase = new RelayCommand(DoEditSharedDatabase, CanEditSharedDatabase);
                }
                return editSharedDatabase;
            }
        }

        public ICommand DeleteSharedDatabase
        {
            get
            {
                if (deleteSharedDatabase == null)
                {
                    deleteSharedDatabase = new RelayCommand(DoDeleteSharedDatabase, CanDeleteSharedDatabase);
                }
                return deleteSharedDatabase;
            }
        }

        public SharedDatabaseListVM(Facade facade)
        {
            this.facade = facade;
            Refresh();
        }

        private void Refresh()
        {
            Dictionary<int, UserData> users = facade.GetAllUsers().ToDictionary(p => p.UserId);
            Dictionary<int, DatabaseData> database = facade.GetAllDatabases().ToDictionary(p => p.DatabaseId);
            sharedDatabases.Clear();
            sharedDatabases.AddRange(facade.GetAllSharedDatabases().Select(p => new SharedDatabaseReadVM(
                sharedDatabase: p, 
                ownerUsername: users[database[p.DatabaseId].OwnerUserId].Username, 
                sharedWithUsername: users[p.SharedUserId].Username, 
                databaseName: database[p.DatabaseId].Name, 
                connectionString: database[p.DatabaseId].ConnectionString)));
        }

        private void DoNewSharedDatabase()
        {
            SharedDatabaseCreateView vw = new SharedDatabaseCreateView(new SharedDatabaseCreateVM(facade));
            vw.ShowDialog();
            Refresh();
        }

        private void DoEditSharedDatabase()
        {
            SharedDatabaseEditView vw = new SharedDatabaseEditView(new SharedDatabaseEditVM(facade, selectedSharedDatabase.GetData()));
            vw.ShowDialog();
            Refresh();
        }

        private bool CanEditSharedDatabase()
        {
            return SelectedSharedDatabase != null;
        }

        private void DoDeleteSharedDatabase()
        {
            if (facade.ConfirmDelete("shared database"))
            {
                facade.DeleteSharedDatabase(SelectedSharedDatabase.GetData());
                Refresh();
            }
        }

        private bool CanDeleteSharedDatabase()
        {
            return SelectedSharedDatabase != null;
        }
    }
}
